Mixing with C

Home > Software Notes > Languages > C++

mixing with c

https://isocpp.org/wiki/faq/mixing-c-and-cpp

call c from cpp

In .h header add

#ifdef __cplusplus
extern "C" {
#endif
to start and
#ifdef __cplusplus
}
#endif
to the end of the file.

Or if you do not want to touch the header file, wrap the include in your cpp

extern "C" {
  #include "my_c_header.h"
}

call cpp from c

In .hpp header use

#ifdef __cplusplus
extern "C" {
#endif
    void myfunc();
#ifdef __cplusplus
}
#endif

In .cpp

extern "C" void myfunc() {
  //foo();
}